using System;

using EMan.BL.EntityEditing;
using EMan.BL.Properties;
using EMan.Interface.EnterpriseStructure.Employees;
using EMan.Interface.EntityEditing;

using TrueCode.Core.Aspects;
using TrueCode.Core.Diagnostics;

namespace EMan.BL.EnterpriseStructure
{
  [UsedImplicitly]
  public class EmployeeEditorPresenter : EntityEditorPresenter< IEmployeeEditorView >, IEmployeeEditorPresenter
  {
    private readonly EmployeeEditable _employeeEditable;
    private readonly EntityProperty _departmentTitle;
    private readonly EntityProperty _jobTitle;
    private readonly EntityProperty _nickname;
    private readonly EntityProperty _firstname;
    private readonly EntityProperty _secondname;
    private readonly EntityProperty _surname;

    public EmployeeEditorPresenter( [NotNull] EmployeeEditable employeeEditable, [NotNull] IDiagnostic diagnostic ) : base( diagnostic )
    {
      if( employeeEditable == null ) throw new ArgumentNullException( "employeeEditable" );

      _employeeEditable = employeeEditable;
      _departmentTitle = new EntityProperty< string >( Resources.EmployeeEditor_Comment_DepartmentTitle, () => _employeeEditable.Department.Title );

      _jobTitle = new EntityProperty< string >( Resources.EmployeeEditor_Comment_JobTitle,
                                                () => _employeeEditable.Job == null
                                                          ? Resources.EmployeeEditor_JobTitle_NotChoosed
                                                          : _employeeEditable.Job.Title );

      _nickname = new EntityProperty< string >( Resources.EmployeeEditor_Comment_Nickname,
                                                () => _employeeEditable.NickName,
                                                value => _employeeEditable.NickName = value );

      _firstname = new EntityProperty< string >( Resources.EmployeeEditor_Comment_Firstname,
                                                () => _employeeEditable.Firstname,
                                                value => _employeeEditable.Firstname = value );

      _secondname = new EntityProperty< string >( Resources.EmployeeEditor_Comment_Secondname,
                                                () => _employeeEditable.Secondname,
                                                value => _employeeEditable.Secondname = value );

      _surname = new EntityProperty< string >( Resources.EmployeeEditor_Comment_Surname,
                                                () => _employeeEditable.Surname,
                                                value => _employeeEditable.Surname = value );

      OkCommand.CanExecute = true;
    }

    public override void Dispose(){}

    public IEntityProperty DepartmentTitle
    {
      get { return _departmentTitle; }
    }

    public IEntityProperty JobTitle
    {
      get { return _jobTitle; }
    }

    public IEntityProperty Nickname
    {
      get { return _nickname; }
    }

    public IEntityProperty Firstname
    {
      get { return _firstname; }
    }

    public IEntityProperty Secondname
    {
      get { return _secondname; }
    }

    public IEntityProperty Surname
    {
      get { return _surname; }
    }

    public void ChooseJob()
    {
      _employeeEditable.Job = View.ShowJobChooserView();
      _jobTitle.NotifyValueChanged();
    }
  }
}